package com.yanqu.road.dao.impl.activity.apprentice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.apprentice.data.ApprenticeUserPatronsTalentData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class ApprenticeUserPatronsTalentDataDaoImpl extends TempDataObjectDao<ApprenticeUserPatronsTalentData> {

    public static String getApprenticeUserPatronsTalentDataSql = "select * from `t_u_apprentice_patrons_talent_data` where `userId`=?";
    public Map<Integer, ApprenticeUserPatronsTalentData> getApprenticeUserPatronsTalentDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, ApprenticeUserPatronsTalentData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getApprenticeUserPatronsTalentDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                ApprenticeUserPatronsTalentData data = parse(rs);
                map.put(data.getMallShopType(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getUserPatronsTalentByMallShopTypeSql = "select * from `t_u_apprentice_patrons_talent_data` where `userId`=? and `mallShopType` = ?";
    public ApprenticeUserPatronsTalentData getUserPatronsTalentByMallShopType(long userId, int mallShopType) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserPatronsTalentByMallShopTypeSql);
            int i = 1;
            call.setLong(i++, userId);
            call.setInt(i++, mallShopType);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateApprenticeUserPatronsTalentDataSql = "update `t_u_apprentice_patrons_talent_data` set patronsIds=?,talent1=?,talent2=?,talent3=?,talent4=?,talent5=?,historyTalent1=?,historyTalent2=?,historyTalent3=?,historyTalent4=?,historyTalent5=?,convertPropNum=?,createTime=? where userId=? and mallShopType=?";
    @Override
    protected boolean updateData(ApprenticeUserPatronsTalentData apprenticeUserPatronsTalentData) {
        apprenticeUserPatronsTalentData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(apprenticeUserPatronsTalentData.getPatronsIds())));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getTalent1()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getTalent2()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getTalent3()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getTalent4()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getTalent5()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getHistoryTalent1()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getHistoryTalent2()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getHistoryTalent3()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getHistoryTalent4()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getHistoryTalent5()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getConvertPropNum()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserPatronsTalentData.getMallShopType()));
        if (getDb().executeNoneQuery(updateApprenticeUserPatronsTalentDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertApprenticeUserPatronsTalentDataSql = "insert into `t_u_apprentice_patrons_talent_data`(userId,mallShopType,patronsIds,talent1,talent2,talent3,talent4,talent5,historyTalent1,historyTalent2,historyTalent3,historyTalent4,historyTalent5,convertPropNum,createTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(ApprenticeUserPatronsTalentData apprenticeUserPatronsTalentData) {
        apprenticeUserPatronsTalentData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, apprenticeUserPatronsTalentData.getMallShopType()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(apprenticeUserPatronsTalentData.getPatronsIds())));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getTalent1()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getTalent2()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getTalent3()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getTalent4()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getTalent5()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getHistoryTalent1()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getHistoryTalent2()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getHistoryTalent3()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getHistoryTalent4()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getHistoryTalent5()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getConvertPropNum()));
        para.put(i++, new DbParameter(Types.BIGINT, apprenticeUserPatronsTalentData.getCreateTime()));
        if (getDb().executeNoneQuery(insertApprenticeUserPatronsTalentDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected ApprenticeUserPatronsTalentData parseData(ResultSet rs) throws SQLException {
        ApprenticeUserPatronsTalentData apprenticeUserPatronsTalentData = new ApprenticeUserPatronsTalentData();
        apprenticeUserPatronsTalentData.setUserId(rs.getLong("userId"));
        apprenticeUserPatronsTalentData.setMallShopType(rs.getInt("mallShopType"));
        if (!StringUtils.isNullOrEmpty(rs.getString("patronsIds"))) {
            apprenticeUserPatronsTalentData.setPatronsIds(JSON.parseObject(rs.getString("patronsIds"), new TypeReference<Set<Integer>>() {}));
        }
        apprenticeUserPatronsTalentData.setTalent1(rs.getLong("talent1"));
        apprenticeUserPatronsTalentData.setTalent2(rs.getLong("talent2"));
        apprenticeUserPatronsTalentData.setTalent3(rs.getLong("talent3"));
        apprenticeUserPatronsTalentData.setTalent4(rs.getLong("talent4"));
        apprenticeUserPatronsTalentData.setTalent5(rs.getLong("talent5"));
        apprenticeUserPatronsTalentData.setHistoryTalent1(rs.getLong("historyTalent1"));
        apprenticeUserPatronsTalentData.setHistoryTalent2(rs.getLong("historyTalent2"));
        apprenticeUserPatronsTalentData.setHistoryTalent3(rs.getLong("historyTalent3"));
        apprenticeUserPatronsTalentData.setHistoryTalent4(rs.getLong("historyTalent4"));
        apprenticeUserPatronsTalentData.setHistoryTalent5(rs.getLong("historyTalent5"));
        apprenticeUserPatronsTalentData.setConvertPropNum(rs.getLong("convertPropNum"));
        apprenticeUserPatronsTalentData.setCreateTime(rs.getLong("createTime"));
        return apprenticeUserPatronsTalentData;
    }
}
